<template>
	<view :class="list.length==0?'content':''">
		<view class="" style="height: 100rpx;"></view>
		<view class="title">
			<view :class="titindex==0?'active':''" @click="settitindex(0)"><text>全部</text></view>
			<view :class="titindex==1?'active':''" @click="settitindex(1)"><text>待帮买</text></view>
			<view :class="titindex==2?'active':''" @click="settitindex(2)"><text>帮买中</text></view>
			<view :class="titindex==3?'active':''" @click="settitindex(3)"><text>已完成</text></view>
			<view :class="titindex==4?'active':''" @click="settitindex(4)"><text>已取消</text></view>
		</view>
		<view class="listbg" v-if="list.length>0" v-for="(item,index) in list" :key="index">
			<!-- <template v-if="item.status < 3"> -->
			<template v-if="item.allShoppingList && item.allShoppingList.length > 1">
				<view class="listitem" @click="goinfo(item.id)">
					<view class="tit">
						<text>{{item.market.mar_name || '暂无'}}-{{item.groupOrder.staffSimpleInfo[0].merchantField.mer_name || '暂无'}}</text><text>{{getTitle(item.task_type,item.status)}}</text>
					</view>
					<!-- <view class=""></view>	 -->
					<view class="gray">下单时间: {{item.create_date || '暂无'}}</view>
					<view class="goods">
						<view class="imgbg">
							<image v-if="i <= 3" :src="ele" v-for="(ele,i) in item.allShoppingList" :key="index">
							</image>
							<image v-if="item.allNum >= 3" class="dian" src="../static/icon_296@2x.png" mode=""></image>
						</view>
						<view style="font-size: 12px;" v-if="item.total ==1 && is_name_show">
							{{item.oneProudctName}}
						</view>
						<view class="price">
							<!-- <view class="">￥<text>{{item.groupOrder.pay_price || '0'}}</text></view> -->
							<view class="">￥<text>{{item.m_ || '0'}}</text></view>
							<text>共{{item.total || '1'}}件</text>
						</view>
					</view>
					<view class="goodsbtn" v-if="item.task_type == 1">
						<!-- 正常订单 -->
						<!-- 待帮买 -->
						<view v-if="item.status*1 ==1" @click.stop="atOnceBuy(item.id)">立即接单</view>
						<!-- 帮买中 -->
						<view v-if="item.status*1 ==2">帮买完成</view>
						<!-- 帮买完成 -->
						<view v-if="item.status*1 ==3" @click.stop="look(item)">查看评论</view>
						<!-- 取消任务 -->
						<view v-if="item.status*1 ==4">订单详情</view>
					</view>
					<!-- 退换货订单 -->
					<view class="goodsbtn" v-if="item.task_type == 4 && item.status*1 ==1">
						<!-- 待帮买 -->
						<view @click.stop="atOnceBuy(item.id)">立即接单</view>
					</view>
					<view class="goodsbtn" v-if="item.task_type == 4 && item.status*1 ==2">
						<!-- 待帮买 -->
						<view>帮买完成</view>
					</view>
				</view>
			</template>
			<template v-else>
				<view class="listitem" @click="goinfo(item.id)">
					<view class="tit">
						<text>{{item.market.mar_name || '暂无'}}-{{item.groupOrder.staffSimpleInfo[0].merchantField.mer_name || '暂无'}}</text><text>{{getTitle(item.task_type,item.status)}}</text>
					</view>
					<view class="goods goods2">

						<image :src="item.allShoppingList[0]" mode=""></image>
						<!-- 	<view style="font-size: 12px;" v-if="item.total ==1 && is_name_show">
							{{item.oneProudctName}}
						</view> -->
						<view class="one">
							<text>{{item.oneProudctName}}</text>
							<text class="gray">下单时间: {{item.create_date || '暂无'}}</text>
						</view>
						<view class="price price2">
							<view class="">￥<text>{{item.m_ || '0'}}</text></view>
							<text>共{{item.total || '0'}}件</text>
						</view>
					</view>
					<view class="realpay" v-if="item.RefundInfo">
						<!-- 退换货金额:￥<text>{{item.RefundInfo.refund_price || '0'}}</text> -->
					</view>
					<view class="realpay" v-else>
						实付金额:￥<text>{{item.groupOrder.pay_price || '0'}}</text>
					</view>
					<view class="goodsbtn" v-if="item.task_type == 1">
						<!-- 正常订单 -->
						<!-- 待帮买 -->
						<view v-if="item.status*1 ==1" @click.stop="atOnceBuy(item.id)">立即接单</view>
						<!-- 帮买中 -->
						<view v-if="item.status*1 ==2">帮买完成</view>
						<!-- 帮买完成 -->
						<view v-if="item.status*1 ==3" @click.stop="look(item)">查看评论</view>
						<!-- 取消任务 -->
						<view v-if="item.status*1 ==4">订单详情</view>
					</view>
					<!-- 退换货订单 -->
					<view class="goodsbtn" v-if="item.task_type == 4 && item.status*1 ==1">
						<!-- 待帮买 -->
						<view @click.stop="atOnceBuy(item.id)">立即接单</view>
					</view>
					<view class="goodsbtn" v-if="item.task_type == 4 && item.status*1 ==2">
						<!-- 待帮买 -->
						<view>帮买完成</view>
					</view>
				</view>
			</template>
		</view>
		<view class="nomore" v-if="list.length == 0">
			<image src="../static/icon_294@2x.png" mode=""></image>
			<text>暂时没有任何订单</text>
			<text>稍等一会儿再来吧</text>
		</view>
		<view class="center" v-if="userinfo">

			<image src="../static/icon_295@2x.png" mode="" @click.stop="changezt(3)" v-if="userinfo.staff_status==1">
			</image>
			<image src="../static/icon_303@2x.png" mode="" @click.stop="changezt(1)" v-else></image>
		</view>
		<!-- 底部tabbar -->
		<view class="" id='tab'>
			<u-tabbar :value="value" :fixed="true" :placeholder="true" activeColor="#109C7A">
				<block v-for="(item,index) in tabbar" :key='index'>
					<u-tabbar-item :text="item.name" @click="switchtab(item.url)">
						<image class="u-page__item__slot-icon" slot="active-icon" :src="item.checked_icon">
						</image>
						<image class="u-page__item__slot-icon" slot="inactive-icon" :src="item.no_checked_icon">
						</image>
					</u-tabbar-item>
				</block>
			</u-tabbar>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				userinfo: '',
				// timer:'',//倒计时
				loadingType: 0, //分页
				goData: {
					status: '',
					page: 1,
					task_date: '', //时间段
				}, //提交信息
				value: 0,
				titindex: 0,
				list: [],
				is_name_show: true,
				tabbar: [{
						name: '订单中心',
						checked_icon: '../static/cpm_tab-icon_1@3x.png',
						no_checked_icon: '../static/cpm_tab-icon_3@3x.png',
						url: '/P_buyer/order_center/index'
					},
					{
						name: '个人中心',
						checked_icon: '../static/cpm_tab-icon_2@3x.png',
						no_checked_icon: '../static/cpm_tab-icon_4@3x.png',
						url: '/P_buyer/mine/index'
					}
				]
			}
		},
		watch: {
			//监听状态栏
			titindex(newValue) {
				if (newValue == 0) {
					this.goData.status = ''
				} else {
					this.goData.status = newValue;
				}
				this.loadingType = 0;
				this.goData.page = 1;
				this.list = [];
				this.getList();
			}
		},
		onLoad() {

		},
		onShow() {
			this.goData.page = 1;
			this.loadingType = 0;
			this.list = [];
			console.log('a');
			// 获取列表
			this.getList();
			//更新买手位置
			this.getdingwei();
			clearInterval(getApp().globalData.time88);
			getApp().globalData.time88 = setInterval(e => {
				this.getdingwei();
			}, 30000);
			if (uni.getStorageSync('token')) {
				this.token = uni.getStorageSync('token')
				this.getuserinfo()
			}
		},
		//上拉加载
		onReachBottom: function() {
			if (this.loadingType == 2) {
				return uni.showToast({
					title: '没有更多了',
					icon: 'none'
				})
			}
			if (this.loadingType != 0) { //loadingType!=0;直接返回
				return false;
			}
			this.goData.page++;
			uni.showNavigationBarLoading(); //显示加载动画				
			this.getList(); //请求
		},
		methods: {
			// 切换状态
			changezt(e) {
				let that = this
				uni.showModal({
					title: '提示',
					content: '是否切换状态',
					success: function(res) {
						if (res.confirm) {
							that.$request("buyer/set_status", {
								status: e
							}, 'GET').then(res => {
								that.getuserinfo()
							})
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},
			getuserinfo() {
				this.$request('user', {}, 'GET').then(res => {
					console.log(res, '信息');
					this.userinfo = res.data
				})
			},
			//更新买手定位
			getdingwei() {
				uni.getLocation({
					type: 'gcj02 ',
					success: res => {
						// console.log('当前位置的经度：' + res.longitude);
						// console.log('当前位置的纬度：' + res.latitude);
						this.$request('update_gps', {
							longitude: res.longitude,
							latitude: res.latitude,
						}, 'POST').then(ele => {
							console.log(ele, 'ele');
						})
					}
				});
			},
			// 查看评论
			look(item) {
				let data = JSON.stringify(item);
				uni.setStorageSync('data55', data);
				uni.navigateTo({
					url: "/P_buyer/mine/evaluate_look"
				})
			},
			// 立即接单
			atOnceBuy(task_id) {
				uni.requestSubscribeMessage({
					tmplIds: ['8weYiNMM30JxdGsHWDAVR8OZmtUFS0NtYHnmITPa_J4'],
					success:re=> {
						uni.showModal({
							title: '提示',
							content: '确认接单吗?',
							success: res => {
								if (res.confirm) {
									this.$request('buyer/accept', {
										task_id,
									}, 'POST').then(ele => {
										console.log(ele, 'ele');
										if (ele.status == 200) {
											this.titindex = 2;
										}
									})
								} else if (res.cancel) {
									console.log('用户点击取消');
								}
							}
						});
					},
					fail:err=> {
						uni.showModal({
							title: '提示',
							content: '确认接单吗?',
							success: res => {
								if (res.confirm) {
									this.$request('buyer/accept', {
										task_id,
									}, 'POST').then(ele => {
										console.log(ele, 'ele');
										if (ele.status == 200) {
											this.titindex = 2;
										}
									})
								} else if (res.cancel) {
									console.log('用户点击取消');
								}
							}
						});
					}
				})
				
				
				
				
			},
			getTitle(type, index) {
				// 状态名获取
				if (type * 1 == 1) {
					switch (index * 1) {
						case 0:
							return '待指定';
						case 1:
							return '待帮买';
						case 2:
							return '帮买中';
						case 3:
							return '帮买完成';
						case 4:
							return '已取消';
					}
				}
				if (type * 1 == 4) {
					switch (index * 1) {
						case 0:
							return '退/换货待指定';
						case 1:
							return '退/换货待处理';
						case 2:
							return '退/换货处理中';
						case 3:
							return '退/换货处理完成';
						case 4:
							return '退/换货取消任务';
					}
				}
			},
			getList() {
				this.$request('buyer/task_lst', {
					...this.goData,
				}, 'GET').then(res => {
					if (res.status == 200) {
						this.list.push(...res.data.data);
						uni.hideNavigationBarLoading(); //关闭加载动画
						//如果长度小于等于0说明没有分页
						if (res.data.data.length < 15) {
							this.loadingType = 2; //没有更多了	
						} else {
							this.loadingType = 0; //请求成功让他返回上拉加载	
						}
						// 循环外层任务订单  
						this.list.forEach((ele, index) => {
							let m_ = 0
							ele.refundProudctIds = [] //退换货商品
							// if (ele.RefundInfo) {
							// 	ele.RefundInfo.forEach(info=>{
							// 		info.refundProduct.forEach(refundProduct => {
							// 			ele.refundProudctIds.push(refundProduct.order_product_id)
							// 		})
							// 	})
							// }
							if (ele.RefundInfo) {
								ele.RefundInfo.refundProduct.forEach(refundProduct => {
										ele.refundProudctIds.push(refundProduct.order_product_id)
									})
							}
							
							let num = 0; //商品种类数量
							let total = 0; //商品总数
							let shoppingList = []; //商品图片
							let shoppingText = []; //介绍
							//循环商品总订单里面的子订单
							ele.groupOrder.staffSimpleInfo.forEach(ele2 => {
								//商品图片
								ele2.simpleOrderProduct.forEach(ele4 => {
									if (ele.refundProudctIds.length != 0) {
										if (ele.refundProudctIds.indexOf(
												ele4.order_product_id) != -1) {
										m_+= Number(ele4.product_price)
											//商品种类数量
											num += 1;
											//商品总数
											total += ele4.product_num
											shoppingList.push(
												ele4.simpleCartInfo.image
											);
											shoppingText.push(
												ele4.simpleCartInfo.simpleCartInfo
											);
											this.$set(this.list[index], 'oneProudctName',
												ele4.simpleCartInfo.store_name);
										}

									} else {
										//商品种类数量
										// num += ele2.simpleOrderProduct.length;
										num += 1;
										//商品总数
										total += ele4.product_num
										this.$set(this.list[index], 'oneProudctName',
											ele4.simpleCartInfo.store_name);
										shoppingText.push(
											ele4.simpleCartInfo.simpleCartInfo
										);
										shoppingList.push(
											ele4.simpleCartInfo.image
										);
										m_+= Number(ele4.product_price)
									}

								})
							})
							this.$set(this.list[index], 'allNum', num);
							this.$set(this.list[index], 'm_', m_.toFixed(2));
							this.$set(this.list[index], 'allShoppingList', shoppingList);
							this.$set(this.list[index], 'total', total);
							this.$set(this.list[index], 'shoppingText', shoppingText);
						})
						console.log(this.list)
					}
				})
				console.log(this.list, '图片');
			},
			goinfo(id) {
				uni.navigateTo({
					url: "./orderinfo?id=" + id
				})
			},
			settitindex(e) {
				this.titindex = e
			},
			switchtab(e) {
				uni.redirectTo({
					url: e
				})
			},

		}
	}
</script>


<style>
	.listbg {
		width: 690rpx;
		margin-left: 30rpx;


	}

	.realpay {
		font-size: 24rpx;
		font-family: Microsoft YaHei;
		font-weight: 400;
		color: #333333;
		display: flex;
		justify-content: flex-end;
		align-items: baseline;
		margin-top: 6rpx;
	}

	.realpay text {
		font-size: 36rpx;
		font-family: Microsoft YaHei;
		font-weight: 400;
		color: #333333;
	}

	.goodsbtn {
		width: 100%;
		display: flex;
		justify-content: flex-end;
		margin-top: 20rpx;
	}

	.goodsbtn>view {
		width: 160rpx;
		height: 60rpx;
		background: linear-gradient(64deg, #2BCCA5, #22B591);
		box-shadow: 0px 4rpx 6rpx 0px rgba(20, 187, 146, 0.4);
		border-radius: 10rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 24rpx;
		font-family: Microsoft YaHei;
		font-weight: 400;
		color: #FFFFFF;
	}

	.listitem {
		width: 690rpx;
		height: auto;
		background: #FFFFFF;
		border-radius: 20rpx;
		margin-top: 30rpx;
		box-sizing: border-box;
		padding: 30rpx;
	}

	.tit {
		display: flex;
		justify-content: space-between;
		font-size: 26rpx;
		align-items: center;
		font-family: Microsoft YaHei;
		font-weight: 400;
		color: #333333;
	}

	.tit text:nth-of-type(2) {
		font-size: 26rpx;
		font-family: Microsoft YaHei;
		font-weight: 400;
		color: #F83535;
	}

	.gray {
		font-size: 22rpx;
		font-family: Microsoft YaHei;
		font-weight: 400;
		color: #999999;
		margin-top: 10rpx;
	}

	.price2>view,
	.price2>view text {
		font-size: 26rpx !important;
		font-family: Microsoft YaHei;
		font-weight: 400 !important;
		color: #333333 !important;
	}

	.price2>text {
		margin-top: 4rpx;
		font-size: 22rpx !important;
		font-family: Microsoft YaHei;
		font-weight: 400;
		color: #999999 !important;
	}

	.goods2>image {
		width: 140rpx;
		height: 140rpx;
		border-radius: 10rpx;
		/* background: #18BC37; */
		margin-right: 20rpx;

	}

	.one {
		display: flex;
		flex-direction: column;
	}

	.one>text:nth-of-type(1) {
		font-size: 26rpx;
		font-family: Microsoft YaHei;
		font-weight: 400;
		color: #333333;
		line-height: 36rpx
	}

	.goods {
		display: flex;
		align-items: center;
		justify-content: space-between;
		margin-top: 12rpx;
	}

	.price {
		display: flex;
		flex-direction: column;
		align-items: flex-end;
		font-size: 24rpx;
		font-family: Microsoft YaHei;
		font-weight: 400;
		color: #F83535;
	}

	.price>view text {
		font-size: 36rpx;

		font-weight: bold;

	}

	.price>text {
		font-size: 22rpx;
		font-family: Microsoft YaHei;
		font-weight: 400;
		color: #333333;
	}

	.imgbg {
		display: flex;
		align-items: center;
	}

	.imgbg .dian {
		width: 30rpx;
		height: 8rpx;
	}

	.imgbg image {
		width: 100rpx;
		height: 100rpx;
		border-radius: 10rpx;
		/* background: #18BC37; */
		margin-right: 10rpx;
	}

	.center {
		position: fixed;
		right: 0rpx;
		bottom: 180rpx;
		width: 150rpx;
		height: 144rpx;
		z-index: 6;

	}

	.center image {
		width: 100%;
		height: 100%;
	}

	.content {
		width: 100%;
		/* height: 100vh;
		background: #FFFFFF; */

	}

	.nomore {
		display: flex;
		flex-direction: column;
		align-items: center;
		font-size: 26rpx;
		font-family: Microsoft YaHei;
		font-weight: 400;
		color: #666666;
		overflow: hidden;
		/* margin-top: 300rpx; */
	}

	.nomore image {
		width: 306rpx;
		height: 352rpx;
		margin-top: 90rpx;
	}

	.nomore text:nth-of-type(1) {
		font-size: 34rpx;
		font-family: Microsoft YaHei;
		font-weight: bold;
		color: #333333;
		margin-bottom: 30rpx;
	}

	page {
		background: #F5F5F5;
	}

	.title {
		width: 750rpx;
		height: 100rpx;
		background: #FFFFFF;
		display: flex;
		align-items: center;
		justify-content: space-between;
		font-size: 26rpx;
		font-family: Microsoft YaHei;
		font-weight: 400;
		color: #666666;
		box-sizing: border-box;
		padding: 0 30rpx;
		position: fixed;
		top: 0;
		z-index: 4;
	}

	.active {
		position: relative;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.active text {
		font-size: 26rpx;
		font-family: Microsoft YaHei;
		font-weight: bold;
		color: #333333;
	}

	.active::after {
		content: '';
		width: 50rpx;
		position: absolute;
		height: 8rpx;
		background: linear-gradient(64deg, #2BCCA5, #22B591);
		border-radius: 4rpx;
		top: 44rpx;
	}
</style>
